var editIndex = undefined;
var courseObject = new Object_EasyuiGrid('dg_course');

function submitForm() {
    endEditing();// 先结束Grid编辑状态
    $.messager.confirm('确认项', '请确认是否保存数据库', function(r) {
        if (r) {
            $('#form_course').form({
                url : path_home + '/course/insertCourse.json',
                onSubmit : function() {
                    var datas = courseObject.getData();// Grid的数据
                    var mydata = [];
                    for (var i = 0; i < datas.total; i++) {
                        if (datas.rows[i]['course2Day'] && datas.rows[i]['classroomName']) {// 对有值的列
                            mydata.push(datas.rows[i]);// 加入我们的数组
                        }
                    }
                    courseObject.validateAllCumlun();
                    $("#bodyData").val(JSON.stringify(mydata));// 把表体数据转成JSON字符串，并加入表单
                    return $(this).form('enableValidation').form('validate');
                },
                success : function(data) {
                    var d = typeof data== 'string'? JSON.parse(data):data;
                    $.messager.show({
                        title : '操作提示',
                        msg : d.msg,
                        timeout : 3000,
                        showType : 'slide'
                    });
                }
            });
            $('#form_course').submit();
        }
    });
}
function clearForm() {
    $('#form_course').form('clear');
}

// ---------------------------对Grid表格的本地处理---------------------------
// 结束编辑状态
function endEditing() {
    if (courseObject.endEditing(editIndex)) {
        editIndex = undefined;
        return true;
    }
    return false;
}
// 点击单元格时
function onClickCell(index, field) {
    if (editIndex != index) {
        if (endEditing()) {
            courseObject.selectRow(index);// 设置选择状态
            courseObject.beginEdit(index, field, editIndex);// 进行编辑状态
            editIndex = index;
        }
    }
}

//根据日期，取得星期几
function getWeekByDate(strDate){
    if(strDate==''||strDate==null){
        return '';
    }
    var date;
    var mydate = strDate.split(/[-/]{1}/);
    date = new Date(mydate[0], parseInt(mydate[1] - 1), mydate[2]);
    return "星期" + "日一二三四五六".charAt(date.getDay());
};


// 事件。结束编辑前一刻
function onEndEdit(index, row) {
    var ded = courseObject.getEditor(index, 'course2Day');
    var dtext = $(ded.target).combobox('getText');//取得显示内容
    var dvalue =$(ded.target).combobox('getValue');//取得值
    var myWeek=getWeekByDate(dvalue);

    //取classroomName 编辑框对象
    var ced = courseObject.getEditor(index, 'classroomName');
    var ctext = $(ced.target).combobox('getText');//取得显示内容
    var cvalue = $(ced.target).combobox('getValue');//取得值

    //取teacherName 编辑框对象
    var ted = courseObject.getEditor(index, 'teacherName');
    var ttext = $(ted.target).combobox('getText');//取得显示内容
    var tvalue = $(ted.target).combobox('getValue');//取得值
    var celldata={};
    celldata.course2Week=myWeek;
    if(ctext!=cvalue){
        celldata.classroomId=cvalue;
        celldata.classroomName=cvalue==''?'':ctext;
    }
    if(ttext!=tvalue){
        celldata.teacherId=tvalue;
        celldata.teacherName=tvalue==''?'':ttext;
    }
    courseObject.updateCell(index,celldata);
}
// 删除行
function dellRow() {
    endEditing();// 结束编辑
    courseObject.deleteRow();// 删除
}
// 添加行
function addRow() {
    endEditing();// 结束编辑
    var index = courseObject.addRow({
        // course2_name : ''
    });// 加入一行
    courseObject.selectRow(index);// 设置选择状态
    courseObject.beginEdit(index, 'courseName');// 进行编辑状态
    editIndex = index;// 记录当前编辑的坐标
}
// 上移
function MoveUp() {
    courseObject.MoveUp();
}
// 下移
function MoveDown() {
    courseObject.MoveDown();
}